package cn.iocoder.yudao.module.product.service.categoryPlatform;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.categoryPlatform.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.categoryPlatform.CategoryPlatformDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.categoryPlatform.CategoryPlatformMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 平台商品分类 Service 实现类
 *
 * @author sqalong
 */
@Service
@Validated
public class CategoryPlatformServiceImpl implements CategoryPlatformService {

    @Resource
    private CategoryPlatformMapper categoryPlatformMapper;

    @Override
    public Long createCategoryPlatform(CategoryPlatformSaveReqVO createReqVO) {
        // 校验父分类编号的有效性
        validateParentCategoryPlatform(null, createReqVO.getParentId());
        // 校验分类名称的唯一性
        validateCategoryPlatformNameUnique(null, createReqVO.getParentId(), createReqVO.getName());

        // 插入
        CategoryPlatformDO categoryPlatform = BeanUtils.toBean(createReqVO, CategoryPlatformDO.class);
        categoryPlatformMapper.insert(categoryPlatform);

        // 返回
        return categoryPlatform.getId();
    }

    @Override
    public void updateCategoryPlatform(CategoryPlatformSaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryPlatformExists(updateReqVO.getId());
        // 校验父分类编号的有效性
        validateParentCategoryPlatform(updateReqVO.getId(), updateReqVO.getParentId());
        // 校验分类名称的唯一性
        validateCategoryPlatformNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());

        // 更新
        CategoryPlatformDO updateObj = BeanUtils.toBean(updateReqVO, CategoryPlatformDO.class);
        categoryPlatformMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategoryPlatform(Long id) {
        // 校验存在
        validateCategoryPlatformExists(id);
        // 校验是否有子平台商品分类
        if (categoryPlatformMapper.selectCountByParentId(id) > 0) {
            throw exception(CATEGORY_PLATFORM_EXITS_CHILDREN);
        }
        // 删除
        categoryPlatformMapper.deleteById(id);
    }


    private void validateCategoryPlatformExists(Long id) {
        if (categoryPlatformMapper.selectById(id) == null) {
            throw exception(CATEGORY_PLATFORM_NOT_EXISTS);
        }
    }

    private void validateParentCategoryPlatform(Long id, Long parentId) {
        if (parentId == null || CategoryPlatformDO.PARENT_ID_ROOT.equals(parentId)) {
            return;
        }
        // 1. 不能设置自己为父平台商品分类
        if (Objects.equals(id, parentId)) {
            throw exception(CATEGORY_PLATFORM_PARENT_ERROR);
        }
        // 2. 父平台商品分类不存在
        CategoryPlatformDO parentCategoryPlatform = categoryPlatformMapper.selectById(parentId);
        if (parentCategoryPlatform == null) {
            throw exception(CATEGORY_PLATFORM_PARENT_NOT_EXITS);
        }
        // 3. 递归校验父平台商品分类，如果父平台商品分类是自己的子平台商品分类，则报错，避免形成环路
        if (id == null) { // id 为空，说明新增，不需要考虑环路
            return;
        }
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            // 3.1 校验环路
            parentId = parentCategoryPlatform.getParentId();
            if (Objects.equals(id, parentId)) {
                throw exception(CATEGORY_PLATFORM_PARENT_IS_CHILD);
            }
            // 3.2 继续递归下一级父平台商品分类
            if (parentId == null || CategoryPlatformDO.PARENT_ID_ROOT.equals(parentId)) {
                break;
            }
            parentCategoryPlatform = categoryPlatformMapper.selectById(parentId);
            if (parentCategoryPlatform == null) {
                break;
            }
        }
    }

    private void validateCategoryPlatformNameUnique(Long id, Long parentId, String name) {
        CategoryPlatformDO categoryPlatform = categoryPlatformMapper.selectByParentIdAndName(parentId, name);
        if (categoryPlatform == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的平台商品分类
        if (id == null) {
            throw exception(CATEGORY_PLATFORM_NAME_DUPLICATE);
        }
        if (!Objects.equals(categoryPlatform.getId(), id)) {
            throw exception(CATEGORY_PLATFORM_NAME_DUPLICATE);
        }
    }

    @Override
    public CategoryPlatformDO getCategoryPlatform(Long id) {
        return categoryPlatformMapper.selectById(id);
    }

    @Override
    public List<CategoryPlatformDO> getCategoryPlatformList(CategoryPlatformListReqVO listReqVO) {
        return categoryPlatformMapper.selectList(listReqVO);
    }

}